package com.scy.bytebit;

/**
 * ByteBit
 *
 * @Author: 马伟奇
 * @CreateTime: 2020-05-05
 * @Description:
 */
public class ByteBit {
    public static void main(String[] args) {
        String a = "a";
        byte[] bytes = a.getBytes();
        for (byte aByte : bytes) {
            int c = aByte;
            System.out.println(c);
            // byte 字节，对应的bit是多少
            String s = Integer.toBinaryString(c);
            printIntInBinaryForm(c);
            System.out.println(s);
        }
    }

    public static void printIntInBinaryForm(int aInt) {
        int result = aInt & 0xff;
        String binaryString = Integer.toBinaryString(result);
        int length = binaryString.length();
        int paddingLength = 32 - length;
        int j=1;
        for (int i = 0; i < paddingLength; i++) {

            System.out.print("0");
            if (j % 8 == 0) {
                System.out.print(" ");
            }
            j++;
        }
        System.out.println(binaryString);
    }
}